package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.BillDetailDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class BillDetailDAO {
	protected static final String TABLE_NAME = "Bill_detail";
	protected static final String ID = "rowid";
	protected static final String BLL_ID = "bll_id";
	protected static final String MN_ID = "mn_id";
	protected static final String TTL_AMNT = "ttl_amnt";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ BLL_ID + " text, " + MN_ID + " text, " + TTL_AMNT + " integer);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public BillDetailDAO(Context context) {
		this.context = context;
	}

	public ArrayList<BillDetailDTO> getAll() {
		ArrayList<BillDetailDTO> billDetailDTOs = new ArrayList<BillDetailDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				BillDetailDTO billDetailDTO = this
						.cursorToBillDetailDTO(cursor);
				billDetailDTOs.add(billDetailDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return billDetailDTOs;
	}

	public BillDetailDTO get(int id) {
		BillDetailDTO billDetailDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			billDetailDTO = this.cursorToBillDetailDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return billDetailDTO;
	}

	public void insert(BillDetailDTO billDetailDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this
				.billDetailDTOToContentValues(billDetailDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(BillDetailDTO billDetailDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = billDetailDTO.getId();
		billDetailDTO.setId(-1);
		ContentValues contentValues = this
				.billDetailDTOToContentValues(billDetailDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private BillDetailDTO cursorToBillDetailDTO(Cursor cursor) {
		BillDetailDTO billDetailDTO = new BillDetailDTO();
		int id = cursor.getInt(cursor.getColumnIndex(ID));
		billDetailDTO.setId(id);
		String bllId = cursor.getString(cursor.getColumnIndex(BLL_ID));
		billDetailDTO.setBllId(bllId);
		String mnId = cursor.getString(cursor.getColumnIndex(MN_ID));
		billDetailDTO.setMnId(mnId);
		int ttlAmnt = cursor.getInt(cursor.getColumnIndex(TTL_AMNT));
		billDetailDTO.setTtlAmnt(ttlAmnt);
		return billDetailDTO;
	}

	private ContentValues billDetailDTOToContentValues(
			BillDetailDTO billDetailDTO) {
		ContentValues contentValues = new ContentValues();
		int id = billDetailDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String bllId = billDetailDTO.getBllId();
		if (bllId != null) {
			contentValues.put(BLL_ID, bllId);
		}
		String mnId = billDetailDTO.getMnId();
		if (mnId != null) {
			contentValues.put(MN_ID, mnId);
		}
		int ttlAmnt = billDetailDTO.getTtlAmnt();
		if (ttlAmnt >= 0) {
			contentValues.put(TTL_AMNT, ttlAmnt);
		}
		return contentValues;
	}
}